package ar.republicagroup.gestion.util.jfreechart;

import java.awt.Color;
import java.awt.Font;
import java.text.DecimalFormat;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.apache.log4j.Logger;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.PieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

/**
 *
 * @author DIEGOALV
 */
public class PieChart3D extends JFrame {
    
    private static Logger log = Logger.getLogger(PieChart3D.class);
    
    
    public PieChart3D(Map<String,Object> data,String title) {
        super(title);
        setContentPane(createDemoPanel(data,title));
        
    }
    
    
    public static PieDataset createDataSet(Map<String,Object> data){
        
        DefaultPieDataset dataset = new DefaultPieDataset();
        
        try{
        
            for(Map.Entry<String,Object> row : data.entrySet()){
                dataset.setValue(row.getKey(), Double.parseDouble(row.getValue().toString()));
            }
        
        }catch(Exception ex){
            log.error("Error generandto Dataset", ex);
        }
        
        return dataset;
    }
    
    public static JFreeChart createChart3D(PieDataset dataset,String chartTitle) {
        
        JFreeChart chart = ChartFactory.createPieChart3D(
            chartTitle,          // chart title
            dataset,             // data
            true,                // include legend
            true,
            true
        );

        
        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        plot.setForegroundAlpha(0.5f);
        plot.setBackgroundAlpha(0.2f);
 
        chart.setBackgroundPaint(Color.white);
        chart.setAntiAlias(true);
        chart.setBorderVisible(true);
        
        PieSectionLabelGenerator gen = new StandardPieSectionLabelGenerator(
            "{0}: {1} ({2})", new DecimalFormat("$###,###.##"), new DecimalFormat("0%"));
        plot.setLabelGenerator(gen);
        
        final Rotator rotator = new Rotator(plot);
        rotator.start();
        
        return chart;
        
    }
    
     public static JPanel createDemoPanel(Map<String,Object> data,String charTitle) {
        JFreeChart chart;
        chart = createChart3D(createDataSet(data),charTitle);
        return new ChartPanel(chart);
    }
     
    
}
